package com.by.service.impl;

import com.by.bean.ForwardInquiry;
import com.by.bean.Quote;
import com.by.bean.ResultBean;
import com.by.crud.BaseServiceImpl;
import com.by.mapper.ForwardInquiryMapper;
import com.by.mapper.QuoteMapper;
import com.by.service.QuoteService;
import com.by.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

/**
 * date 2025/3/4 15:23
 * author bjj
 * decoration
 * version 1.0
 * since jdk11
 */
@Service
public class QuoteServiceImpl extends BaseServiceImpl<Quote, QuoteMapper> implements QuoteService {
    @Autowired
    QuoteMapper quoteMapper;
    @Autowired
    ForwardInquiryMapper forwardInquiryMapper;



    @Override
    @Transactional
    public int save(Quote quote) {
        quote.setSn(getSn());
        int rs =super.save(quote);
        Assert.isZero(rs,"保存失败");
        forwardInquiryMapper.updateStatus(quote.getRelationSn());
        return 1;
    }



    String getSn(){
        String maxSn=quoteMapper.getMaxSn();
        String month = String.format("%02d", LocalDate.now().getMonthValue());
        if(maxSn==null|| maxSn.isEmpty()){
            return "bj"+ LocalDate.now().getYear()+month+"001";
        }else{
            int num = Integer.parseInt(maxSn);
            int rs = num+1;
            return "bj"+ LocalDate.now().getYear()+month+String.format("%03d", rs);
        }
    }

    @Override
    public Quote getByRelationSn(String sn) {
        return quoteMapper.getByRelationSn(sn);
    }
}
